<?php
class Template{
	private $Header,$Body,$Footer,$RepStr;
	public function Template($withheader = true,$withfooter = true){
		$csspath = str_replace(array(ROOT_PATH,"\\"),array("","/"),CSS_PATH);
		$jspath = str_replace(array(ROOT_PATH,"\\"),array("","/"),JS_PATH);
		$imgpath = str_replace(array(ROOT_PATH,"\\"),array("","/"),IMG_PATH);
		$this->RepStr = array(
				"##SEARCHINPUT#"=> "SearchInput.html",
				//default setting
				"##CSSPATH#"	=> $csspath,
				"##JSPATH#"		=> $jspath,
				"##IMGPATH#"	=> $imgpath,
				"##SITEURL#"	=> HTTP_URL,
				"##YAHOO_AUCTION_URL#"	=> YAHOO_AUCTION_URL,
				"##SHOPPING_URL#"	=> SHOPPING_URL,
				"##YAHOO_SNIPER_URL#"	=> YAHOO_SNIPER_URL,
				"##WATCHLIST_URL#"	=> WATCHLIST_URL,
				"##CALCULATOR_URL#"	=> CALCULATOR_URL,
				"\n"			=> NULL,
				"  "			=> NULL,
				"	"			=> NULL
			);
		$header_path = "header.html";
		$footer_path = "footer.html";
		$this->Header = NULL;
		$this->Footer = NULL;
		if($withheader){
      $this->Header = $this->GetHtml($header_path);
		}
		if($withfooter){
      $this->Footer = $this->GetHtml($footer_path);
		}
	}
	private function GetHtml($path){
		$html = NULL;
		$path = HTML_PATH.$path;
		if(file_exists($path)){
			$html = file_get_contents($path,FILE_USE_INCLUDE_PATH);
		}
		return $html;
	}
	public function Out($path,$data = array()){
		if(!empty($data)){
			$this->RepStr = array_merge($data,$this->RepStr);
		}
		$rep_key = array();
		$rep_val = array();
		foreach($this->RepStr as $key=>$val){
			array_push($rep_key,$key);
			if(strpos($val, ".html")){
				$val = $this->GetHtml($val);
			}
			array_push($rep_val,$val);
		}
		$body = $this->Header.$this->GetHtml($path).$this->Footer;
		echo str_replace($rep_key,$rep_val,$body);
	}
}
?>